package easy;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author admin
 * 1710. 卡车上的最大单元数
 * 解题思路：贪心算法思想，按列将二维数组中的元素从大到小排序，从大到小取箱子，直到达到容量
 */
public class LeeCode1710 {

    public int maximumUnits(int[][] boxTypes, int truckSize){

        int sum = 0;
        Arrays.sort(boxTypes, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o2[1] - o1[1];
            }
        });

        for (int i = 0; i < boxTypes.length; i++) {
            if (boxTypes[i][0] <= truckSize){
                truckSize = truckSize - boxTypes[i][0];
                sum = sum + boxTypes[i][0]*boxTypes[i][1];
            }else {
                sum = sum + boxTypes[i][1]*truckSize;
                break;
            }
        }

        return sum;
    }

    public static void main(String[] args) {

        int[][] boxTypes = {{5,10},{2,5},{4,7},{3,9}};
        System.out.println(new LeeCode1710().maximumUnits(boxTypes,10));
    }
}
